package com.dailyblue.java.servlet.jsp.dao;

import com.dailyblue.java.servlet.jsp.bean.Dept;
import com.dailyblue.java.servlet.jsp.db.DBManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeptDAO {
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;

    public List<Dept> findAllDept(){
        String sql = "select * from dept";
        List<Dept> list = new ArrayList<>();
        try {
            conn = DBManager.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Dept dept = new Dept();
                dept.setDeptNo(rs.getInt(1));
                dept.setLoc(rs.getString(3));
                dept.setDname(rs.getString(2));
                list.add(dept);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
